package com.kai.mqtt.protocol;

import com.kai.mqtt.constant.AttributeKeyConstant;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author weijk
 * @since 2024/6/3
 */
@Slf4j
@Component
public class PubRel extends AbstractProtocol {

    @Override
    public void process(Channel channel, MqttMessage message) {
        MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) message.variableHeader();
        log.debug("receive - pubRel, clientId: {}, messageId: {}", channel.attr(AttributeKeyConstant.CLIENT_ID).get(), variableHeader.messageId());

        MqttMessage pubCompMessage = MqttMessageFactory.newMessage(
                new MqttFixedHeader(MqttMessageType.PUBCOMP, false, MqttQoS.AT_MOST_ONCE, false, 0),
                MqttMessageIdVariableHeader.from(variableHeader.messageId()), null);
        log.debug("send - pubComp, clientId: {}, messageId: {}", channel.attr(AttributeKeyConstant.CLIENT_ID).get(), variableHeader.messageId());
        channel.writeAndFlush(pubCompMessage);
    }
}
